package work.person.groups;

import work.config.Constant;
import work.fram.KeyValueMapper;
import work.fram.SqlMaker;
import work.getter.Dept;
import work.person.PersonGW;
import work.person.SqlHandler;
import work.person.helper.CheckHelper;
import work.person.helper.ConditionHelper;
import work.person.helper.LoggerHelper;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class Zb extends AbstractTableHandler {
    public Zb() {
        setExcelFiles("个人信息", "工作信息");

        setName("主表");

        map("备注", "remark");


        //处理为字典值
        Map<String, String> mapMap = new HashMap<>();
        mapMap.put("gender", "性别");

        mapMap.put("degree", "学历字典");
        mapMap.put("jobs", "岗位字典");
        mapMap.put("person_category", "人员类别字典");
        mapMap.put("post_type", "任职类型");
        mapMap.put("technical_position", "专业技术职务字典");
        mapMap.put("devote_major", "从事专业字典");
        mapMap.put("person_sort", "人员分类");
        mapMap.put("entry_sources", "进入来源字典");
        mapMap.put("household_nature", "户口性质字典");
        mapMap.put("politic_face", "政治面貌字典");
        mapMap.put("marital_status", "婚姻状况字典");
        mapMap.put("health_status", "健康状况字典");

        setNameCodeMap(mapMap);

    }

    @Override
    public void mappingFieldDict(Map<String, String> map) {
        super.mappingFieldDict(map);
        String code = map.get("org_code");
        if (code == null) {
            map.remove("org_name");
        } else {
            String name = Dept.getNameByCode(code);
            if (name != null) {
                map.put("org_name", name);
            } else {
                map.remove("org_code");
            }
        }
    }

    private SqlMaker sqlMaker;

    private KeyValueMapper keyValueMapper;

    @Override
    public void handle(Map<String, String> map, String name, String idcard, File file) {

        map.put("备注", file.getParentFile().getName());
        super.handle(map, name, idcard, file);
    }

    @Override
    public void afterHandle(Map<String, String> map) {

        //根据身份证和姓名去重处理
        String name = map.get("name");
        String idcard = map.get("idcard");

        //使用身份证号作为唯一校验
        if (CheckHelper.exist("tc_md_personnel", ConditionHelper.create("name", "=", name))) {
            LoggerHelper.log("tc_md_personnel中已经存在相同的身份证号", name, idcard);
            return;
        }


    /*
        if (ExistsJdbc.existByNameIdCard(name, idcard)) {
            AutoLogger.getINTERFACE().writeLine(getTableName() + "存在重复身份证号和姓名", name, idcard);
        }*/

        //判断身份证号是否已经存在

        if (sqlMaker == null) {
            sqlMaker = new SqlMaker();
            sqlMaker.setTable(getTableName());
            keyValueMapper = new KeyValueMapper();

            //放入时间
            keyValueMapper.putInvariable("create_time", Constant.crateTime);

            sqlMaker.setKeyValueMapper(keyValueMapper);
        }

        keyValueMapper.reset();
        keyValueMapper.put("sysid", ((PersonGW)getGroupsWrapper()).getId());
        keyValueMapper.put("sysdataid", ((PersonGW)getGroupsWrapper()).getId());

        map.forEach((k,v)-> keyValueMapper.put(k, v));

       // keyValueMapper.put("remark",map.get("remark"));

        //生成sql
        if (sqlMaker.make(SqlMaker.TYPE_INSERT)) {
            SqlHandler.handle(sqlMaker.getSql());
        }


        //SqlHandler.handle(DeWeight.deWeight(getTableName()));

    }

}
